<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/6/30
 * Time: 13:24
 */

namespace app\api\controller\worker;
use app\api\controller\Base;
use app\common\model\AdminModel;
use app\common\model\node\NodeModel;
use app\common\model\node\NodeProcessModel;
use app\common\model\offer\CustomerGoodsDesertModel;
use app\common\model\worker\CustomerAuthModel;
use app\common\model\worker\CustomerBudgetModel;
use app\common\model\worker\CustomerGoodsDataModel;
use app\common\model\worker\CustomerModel;
use app\common\model\worker\CustomerRulerDetailModel;
use app\common\model\worker\CustomerRulerModel;
use app\common\model\worker\RecommendModel;
use app\common\model\worker\TaskModel;
use think\facade\Db;
use think\Request;
use think\Validate;

class CustomerRulerController extends Base
{
    private $model;
    private $customerModel;
    private $nodeModel;
    private $taskModel;
    private $adminModel;
    private $customerRulerDetailModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {

        parent::__construct();
        $this->checkLogin();
        $this->model = new CustomerRulerModel();
        $this->customerModel = new CustomerModel();
        $this->nodeModel = new NodeModel();
        $this->taskModel = new TaskModel();
        $this->adminModel = new AdminModel();
        $this->customerRulerDetailModel = new CustomerRulerDetailModel();

    }
    //量房记录
    public function list(Request $request){
        $param = $request->param();
        $pageSize      = $this->request->param('pageSize/d', 1); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d') ?? 20; //当前页
        //开始校验
        $this->validate($param,[
            'customer_id|客户id'=>'require',

        ]);
        $list = CustomerRulerDetailModel::alias('cal')
            ->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->leftJoin('customer_ruler cr', 'cr.id = cal.ruler_id')
            ->where('cr.customer_id',$param['customer_id'])
            ->order('cal.id','desc')
            ->field('a.nickname,cal.*')
            ->paginate($pageSize, false, ['page' =>$pageNumber ]);

        $this->result($list,'操作成功',1);
    }
    //添加量房
    public function add(Request $request){
        $roles_ids = $this->role_ids;
        if(!in_array(7,$roles_ids)){

            $this->result([],'该用户没有权限，只有业务员有操作权限！',0);
        }
        //$parms = $request->all();
        $param = $request->param();

        //开始校验
        $this->validate($param,[
            'customer_id|客户id'=>'require',
            'goods_data_id|商品id'=>'require',
            'goods_name|商品名称'=>'require',
            'budget_amount|客户预算价'=>'require',
            'finish_time|要求完成时间'=> 'require',
            'admin_id|设计师' => 'require',

        ]);
        $data = [
            'customer_id' => $param['customer_id'],
            'goods_data_id' => $param['goods_data_id'],
            'goods_name' => $param['goods_name'],
            'budget_amount' => $param['budget_amount'],
            'finish_time' => $param['finish_time'],
            'admin_id' =>$param['admin_id'],
            'remark' =>$param['remark'] ?? '',
            'action_admin_id' =>$this->loginInfo['id'],

        ];
        //判断是否已量房
        $info = CustomerGoodsDataModel::where('id',$param['goods_data_id'])->find();
        if($info['ruler_id'] != 0){

            $this->result([],'该客户下的该商品已量房，请勿重复操作！',0);
        }
        if($info['design_status'] >= 1){

            $this->result([],'该客户下的该商品已量房，请勿重复操作！',0);
        }
        Db::startTrans();
        $info = $this->model->add($data);
        //申请量房后进入待量房状态
        $result = CustomerGoodsDataModel::where('id',$param['goods_data_id'])->update(['ruler_id'=>$info['id']]);
        if(!$result){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        if(!$info){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }

        $nodeProcess = NodeProcessModel::where('node_id', 7)->order('id', 'asc')->find();
        $dataTask = [
            'customer_id' => $data['customer_id'],
            'order_id' => 0,
            'node_id' => 7,
            'process_id' => $nodeProcess['id'],
            'receive_admin_id' => $nodeProcess['audit_admin_id'],
            'action_table_id'=>$info['id'],//操作表id
            'admin_id'=>$this->loginInfo['id']
        ];
        $result = $this->taskModel->commonAddTask($dataTask);
        if(!$result){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        Db::commit();
        $this->taskModel->sendTaskInfo($dataTask['receive_admin_id'],$nodeProcess['process_name']);
        $this->result($info,'操作成功',1);





    }
    //完成量房
    public function finishRulertEdit(Request $request){
        $roles_ids = $this->role_ids;
        if(!in_array(6,$roles_ids)){

            $this->result([],'该用户没有权限，只有设计师有操作权限！',0);
        }
        $param = $request->param();
        //开始校验
        $this->validate($param,[
            'customer_id|客户id'=>'require',
            'goods_name|商品名称'=>'require',
            'goods_data_id|商品id'=>'require',
            'detail_imgs|预算明细照片'=> 'require',
            //'ruler_id|量房id'=> 'require',
        ]);


        $customerGoodsDataInfo = CustomerGoodsDataModel::where('id',$param['goods_data_id'])->find();
        if($customerGoodsDataInfo['design_status'] == 1){

            $this->result([],'量房已完成，请勿重复操作！！！',0);
        }
        if($customerGoodsDataInfo['design_status'] != 0){

            $this->result([],'该商品不是待量房状态，请勿重复操作！',0);
        }
        $data = [
            'ruler_id' => $customerGoodsDataInfo['ruler_id'],
            'detail_imgs' => $param['detail_imgs'] ? json_encode($param['detail_imgs'],JSON_UNESCAPED_UNICODE) : '',
            'remark' => $param['final_remark'] ?? '',
            'action_admin_id' =>$this->loginInfo['id'],
        ];
        //添加量房信息
        $result = $this->customerRulerDetailModel->add($data);
        //修改产品为已完成量房详情
        CustomerGoodsDataModel::where('id',$param['goods_data_id'])->update(['ruler_detail_id'=>$result['id'],'design_status'=>1]);

        $this->result($result,'操作成功',1);
    }
    public function getInfo(Request $request)
    {
        $param = $request->param();
        //开始校验
        $this->validate($param, [
            'id|记录id' => 'require',
        ]);
        $id = $param['id'];
        $list = CustomerRulerDetailModel::alias('cal')
            ->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->leftJoin('customer_ruler cr', 'cr.id = cal.ruler_id')
            ->field('a.nickname,cal.*,cr.goods_name')
            ->where('cal.id',$param['id'])
            ->find();
        $this->result($list, '操作成功', 1);
    }


}